﻿'   ScaledCursor.vb
'   WireBear Awesemulator
'
'   Created by Chris Kent
'   Copyright 2011 WireBear. All rights reserved
'
#Region "License Information"
'   WireBear Awesemulator is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   WireBear Awesemulator is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with WireBear Awesemulator - License.txt
'   If not, see <http://www.gnu.org/licenses/>.
#End Region


Public Class ScaledCursor

    'AutoScale
    Public Sub New(CI As CursorInfo, ByVal emulator As Emulator, hWnd As IntPtr)
        _OriginalCursor = CI

        'Determine Emulator Scaling
        Dim EmulatorSize As Size = ScreenShot.WindowSize(hWnd)
        If EmulatorSize.Height >= EmulatorSize.Width Then
            'Portrait
            _hRatio = EmulatorSize.Height / Emulator.Size.Height
            _wRatio = EmulatorSize.Width / Emulator.Size.Width
        Else
            'Landscape
            _hRatio = EmulatorSize.Height / emulator.Size.Width
            _wRatio = EmulatorSize.Width / emulator.Size.Height
        End If

        ScaleImages()
    End Sub

    Public Sub New(CI As CursorInfo, WidthScale As Double, HeightScale As Double)
        _OriginalCursor = CI
        _wRatio = WidthScale
        _hRatio = HeightScale

        ScaleImages()
    End Sub

    Private Sub ScaleImages()
        If _OriginalCursor.IsDummyCursor Then Exit Sub

        'Scale hotspot
        _HotSpot = New Point(_OriginalCursor.HotSpot.X * _wRatio, _OriginalCursor.HotSpot.Y * _hRatio)

        'Scale Main Image
        Using OriginalImage As New Bitmap(_OriginalCursor.ImagePath)
            _Image = New Bitmap(CType(OriginalImage.Size.Width * _wRatio, Integer), CType(OriginalImage.Size.Height * _hRatio, Integer))
            Using g As Graphics = Graphics.FromImage(_Image)
                g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                g.DrawImage(OriginalImage, New Rectangle(0, 0, _Image.Width, _Image.Height), 0, 0, OriginalImage.Width, OriginalImage.Height, GraphicsUnit.Pixel)
            End Using
        End Using

        'Scale Pressed Image
        If _OriginalCursor.DetectPressedEvents Then
            Using OriginalImagePress As New Bitmap(_OriginalCursor.ImagePathPress)
                _ImagePress = New Bitmap(CType(OriginalImagePress.Size.Width * _wRatio, Integer), CType(OriginalImagePress.Size.Height * _hRatio, Integer))
                Using g As Graphics = Graphics.FromImage(_ImagePress)
                    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
                    g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
                    g.DrawImage(OriginalImagePress, New Rectangle(0, 0, _ImagePress.Width, _ImagePress.Height), 0, 0, OriginalImagePress.Width, OriginalImagePress.Height, GraphicsUnit.Pixel)
                End Using
            End Using
        End If
    End Sub


    Private _OriginalCursor As CursorInfo
    Public ReadOnly Property OriginalCursor() As CursorInfo
        Get
            Return _OriginalCursor
        End Get
    End Property

    Private _hRatio As Double
    Public ReadOnly Property HeightRatio() As Double
        Get
            Return _hRatio
        End Get
    End Property

    Private _wRatio As Double
    Public ReadOnly Property WidthRatio() As Double
        Get
            Return _wRatio
        End Get
    End Property

    Public Function FriendlyRatio() As Integer
        Select Case _hRatio
            Case Is < 0.42
                Return 33
            Case Is <= 0.58
                Return 50
            Case Is < 0.7
                Return 66
            Case Is <= 0.88
                Return 75
            Case Else
                Return 100
        End Select
    End Function

    Private _Image As Bitmap
    Public ReadOnly Property Image() As Bitmap
        Get
            Return _Image
        End Get
    End Property

    Private _ImagePress As Bitmap
    Public ReadOnly Property ImagePress() As Bitmap
        Get
            Return _ImagePress
        End Get
    End Property

    Private _HotSpot As Point
    Public ReadOnly Property HotSpot() As Point
        Get
            Return _HotSpot
        End Get
    End Property

End Class
